﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.DataAccess;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Entity;

namespace YiYiTong.Rocket.Business
{
    public class MerchantBiz
    {
        #region 单例模式
        private MerchantBiz()
        {
        }

        private static MerchantBiz _instance;
        private static readonly object _syncObject = new object();

        public static MerchantBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new MerchantBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 通过id获取商户信息
        /// </summary>
        /// <param name="merid"></param>
        /// <returns></returns>
        public MerchantModel GetMerchant(string merId)
        {
            MerchantModel merchant = null; //new MerchantModel();
            string redisKey = "merchant_" + merId;

            merchant = RedisService.Get<MerchantModel>(redisKey);
            //判断redis缓存中是否有数据
            if (merchant == null)
            {
                MerchantInfo m = MerchantDataAccess.Instance.GetMerchant(ConvertHelper.ConvertTo<int>(merId, 0));
                if (m != null && m.merId != 0)
                {
                    merchant = new MerchantModel
                    {
                        merId = m.merId,
                        merName = m.merName,
                        merKey = m.merKey,
                        merStatus = m.merStatus,
                        privateKey = m.privateKey,
                        publicKey = m.publicKey
                    };
                    RedisService.Set(redisKey, merchant);
                }
            }

            return merchant;
        }

    }
}
